#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: ct_datasource.py 
@version:
@time: 2022/03/11 
"""
import allure

from TapClientCore.T06_Tapdata_feature.v2.TPF01_datasource.tpf_datasource_v2 import V2TPFDataSource
from TapClientCore.T06_Tapdata_feature.v2.TPF07_logcollector.tpf_logcollector import TPFLogCollector


class CTDataSource(V2TPFDataSource, TPFLogCollector):

    def _update_to_init_info(self, data, update=None):
        if update is None:
            update = {}
        with allure.step('update数据源'):
            connection_id = self.update_datasource_by_db_info(data, update)

    # 共享挖掘
    def DATA_SOURCE_FOR_LOGMINER(self, data):
        with allure.step('创建开启cdc的数据源'):
            connection_id = self.create_datasource_by_db_info(data)
        # 1. 获取sharecdc全局配置
        with allure.step('获取共享挖掘配置'):
            logcollector_attr = self.get_logcollector_attr()
            data.update(logcollector_attr)
            self._update_to_init_info(data, {})
        with allure.step('加载schema'):
            self.load_datasource_schema_by_db_info(data)
        return connection_id

    # 创建数据源并加载schema
    def DATA_SOURCE_TEMPLATE_CREATE(self, data):
        with allure.step('创建数据源'):
            connection_id = self.create_datasource_by_db_info(data)
            self._update_to_init_info(data, {})
        with allure.step('加载schema'):
            self.load_datasource_schema_by_db_info(data)
        return connection_id

    # 创建或更新数据源并加载schema
    def DATA_SOURCE_TEMPLATE_CREATE_UPDATE(self, data, update):
        with allure.step('创建数据源'):
            connection_id = self.create_datasource_by_db_info(data)
            self._update_to_init_info(data, update)
        with allure.step('加载schema'):
            self.load_datasource_schema_by_db_info(data)
        return connection_id

    # 创建+test+load schema
    def DATA_SOURCE_TEST_CREATE(self, data):
        with allure.step('创建数据源'):
            connection_id = self.create_datasource_by_db_info(data)
        with allure.step('测试并加载schema'):
            self.datasource_test_by_db_info(data)
        return connection_id

    # 删除数据源
    def DATA_SOURCE_TEMPLATE_DELETE(self, data):
        with allure.step('删除数据源'):
            self.delete_datasource_by_db_info(data)

    # 获取connection_id
    def get_datasource_id_by_db_info(self, data):
        with allure.step('创建数据源'):
            connection_id = self.create_datasource_by_db_info(data)
        return connection_id
